package com.example.demoback.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demoback.common.Constants;
import com.example.demoback.common.Result;
import com.example.demoback.entity.Dict;
import com.example.demoback.entity.Role;
import com.example.demoback.mapper.DictMapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;

import com.example.demoback.service.IMenuService;
import com.example.demoback.entity.Menu;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yyd
 * @since 2022-06-29
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    //引入Service
    @Resource
    private IMenuService menuService;

    @Resource
    private DictMapper dictMapper;

    @PostMapping("/save")
    public Result save(@RequestBody Menu menu) {
        //新增或更新
        Boolean flag = menuService.saveOrUpdate(menu);
        if (flag){
            return Result.success(flag);
        }
        return Result.error();
    }


    @GetMapping("/ids")
    public Result findAllIds() {
        return Result.success(menuService.list().stream().map(Menu::getId));
    }

    //根据id删除
    @DeleteMapping("/delete/{id}")  //通过“/user/”+id 的url调用接口
    public Result delete(@PathVariable Integer id) {
        return Result.success(menuService.removeById(id));
    }
    //批量删除
    @PostMapping("/delete/batch")
    public Boolean deleteBatch(@RequestBody List<Integer> ids){
        return menuService.removeByIds(ids);
    }

    //查询所有
    @GetMapping("/select/all")
    public Result findAll(@RequestParam(defaultValue = "") String name) {
        List<Menu> menuList = menuService.findMenus(name);
        return Result.success(menuList);
    }

    //根据id查询
    @GetMapping("select/{id}")
    public Menu findOne(@PathVariable Integer id) {
        return menuService.getById(id);
    }

    //分页查询
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String name) {
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name", name); //模糊查询
//        queryWrapper.orderByDesc("id");
        return Result.success(menuService.page(new Page<>(pageNum, pageSize),queryWrapper));
    }



    @GetMapping("/icons")
    public Result getIcons() {
        QueryWrapper<Dict> queryWrapper = new QueryWrapper<>();
        //根据数据类型查icon图标
        queryWrapper.eq("type", Constants.DICT_TYPE_ICON);
        return Result.success(dictMapper.selectList(queryWrapper));
    }


    public int getInfo(String key) {  //查询出一条数据
        QueryWrapper<Menu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", key);
        int a = 2;
        Menu one = new Menu();
        try {
            one = menuService.getOne(queryWrapper); // 从数据库查询信息
        } catch (Exception e) {
            a = 3;
        }
        if (one == null){
            a = 1;
        }
        return a; //1为不存在数据
        //2为存在一条数据
        //3为存在多条数据
    }


}

